// 将数值转换为Boolean类型
const numberToBoolean = (number) => {
	return number ? true : false;
}

// 将Boolean类型转换为数值类型
const booleanToNumber = (bool) => {
	return bool ? 1 : 0;
}

// 将时间戳转换为时间
const timeFormat = (t) => {
	let time = new Date(t * 1000);
	let y = time.getFullYear();
	let m = time.getMonth() + 1;
	let d = time.getDate();
	let h = time.getHours();
	let mm = time.getMinutes();
	let s = time.getSeconds();
	const add_zero = (value) => {
		return value < 10 ? '0' + value : value;
	};
	return y + '-' + add_zero(m) + '-' + add_zero(d) + ' ' + add_zero(h) + ':' + add_zero(mm) + ':' + add_zero(s);
}

// 时间转换成时间戳（精确到秒）
const timeToStamp = (time) => {
	let d = new Date(time);
	return Date.parse(d) / 1000;
}

// 获取当前时间戳（精确到秒）
const getTimeStamp = () => {
	return ((new Date).getTime() / 1000).toFixed(0);
}

// 获取当前日期时间到秒
const getNowTimeToSecond = () => {
	let d = new Date();
	let y = d.getFullYear();
	let m = d.getMonth() + 1;
	let D = d.getDate();
	let h = d.getHours();
	let M = d.getMinutes();
	let s = d.getSeconds();
	
	if(m < 10) {
		m = '0' + m;
	}
	if(D < 10) {
		D = '0' + D;
	}
	if(h < 10) {
		h = '0' + h;
	}
	if(M < 10) {
		M = '0' + M;
	}
	if(s < 10) {
		s = '0' + s;
	}
	return y + '-' + m + '-' + D + ' ' + h + ':' + M + ':' + s;
}

// 获取当前时间
const getNowTime = () => {
	let d = new Date();
	let h = d.getHours();
	let M = d.getMinutes();
	let s = d.getSeconds();
	
	if(h < 10) {
		h = '0' + h;
	}
	if(M < 10) {
		M = '0' + M;
	}
	if(s < 10) {
		s = '0' + s;
	}
	return h + ':' + M + ':' + s;
}

// 获取当前日期到天
const getNowTimeToDay = () => {
	let d = new Date();
	let y = d.getFullYear();
	let m = d.getMonth() + 1;
	let D = d.getDate();
	
	if(m < 10) {
		m = '0' + m;
	}
	if(D < 10) {
		D = '0' + D;
	}
	return y + '-' + m + '-' + D;
}

// 获取某时间戳当天的开始和结束的时间戳
const getTimeStartEndStamp = () => {
	let ts = getNowTimeToDay();
	ts = (new Date(ts)).getTime() / 1000 - (8 * 60 * 60);
	let te = ts + 24 * 60 * 60 - 1;
	let t = {
		start_stamp: ts,
		end_stamp: te
	};
	return t;
}

// 获取一周中每天的所有开始到结束的时间戳
const getTimeWeekTodayStamp = () => {
	let dat = [];
	let week_start_end_stamp = getTimeWeekStartEndStamp();
	let start_stamp = week_start_end_stamp.week_start_stamp;
	
	for(let i = 0; i < 7; i++)
	{
		let tmp = {
			start_stamp: start_stamp + i * 24 * 60 * 60,
			end_stamp: start_stamp + (i + 1) * 24 * 60 * 60 - 1,
		};
		dat.push(tmp);
	}
	
	return dat;
}

// 获取本周的开始和结束时间戳
const getTimeWeekStartEndStamp = () => {
	// 当天是周几
	let t_day = getDayOfWeek();
	// 当前日期到天
	let t = getNowTimeToDay();
	// 当天 0 点的时间戳
	t = (new Date(t)).getTime() / 1000 - (8 * 60 * 60);
	
	let week_start_stamp = t - ((t_day - 1) * 24 * 60 * 60);
	let week_end_stamp = week_start_stamp + 7 * 24 * 60 * 60 - 1;
	let dat = {
		week_start_stamp,
		week_end_stamp,
	};
	return dat;
}

// 获取当天所属周几
const getDayOfWeek = () => {
	let day = (new Date()).getDay();
	if(day == 0) {
		return 7;
	}
	else {
		return day;
	}
}

// 计算传入当前时间到传入时间戳的间隔天数
const calcStampToNow = (time) => {
	let now = Math.floor((new Date()).getTime() / 1000);
	let old = timeToStamp(time);
	let day = now - old;
	
	day = Math.ceil(day / (24 * 60 * 60));
	
	return day;
}

// 将传入时间戳截取到天并格式化
const stampFormatDay = (stamp) => {
	let d = new Date(stamp * 1000);
	let y = d.getFullYear();
	let m = d.getMonth() + 1;
	let D = d.getDate();
	
	if(m < 10) {
		m = '0' + m;
	}
	if(D < 10) {
		D = '0' + D;
	}
	return y + '-' + m + '-' + D;
}

// 将秒转为小时，--:--:--格式
const secondFormatHours = (s) => {
	// 总分钟
	let m_total = Math.floor(s / 60);
	// 秒
	let ss = s % 60;
	// 小时
	let h = Math.floor(m_total / 60);
	// 分钟
	let m = m_total % 60;
	
	const add_zero = (value) => {
		return value < 10 ? '0' + value : value;
	};
	
	return add_zero(h) + ':' + add_zero(m) + ':' + add_zero(ss);
}

// 将秒转为小时，00.000
const secondToHours = (s) => {
	let h = (s / 3600).toFixed(3);
	return h;
}

// 获取时间戳日期到年
const getStampToYear = (stamp) => {
	let date = new Date(stamp * 1000);
	return date.getFullYear();
}

// 获取时间戳日期到月
const getStampToMonth = (stamp) => {
	let date = new Date(stamp * 1000);
	return date.getMonth() + 1;
}

// 获取时间戳日期到日
const getStampToDay = (stamp) => {
	let date = new Date(stamp * 1000);
	return date.getDate();
}

// 获取本月的开始和结束时间戳
const getTimeMonthStartEndStamp = () => {
	let date = new Date();
	// 当天是本月第几天
	let t_day = date.getDate();
	// 当前日期到天
	let t = getNowTimeToDay();
	// 当天 0 点的时间戳
	let now_stamp = (new Date(t)).getTime() / 1000 - (8 * 60 * 60);

	let month_start_stamp = now_stamp - ((t_day - 1) * 24 * 60 * 60);
	let month_end_stamp = month_start_stamp + getNowMonthDayCount() * 24 * 60 * 60 - 1;
	let dat = {
		month_start_stamp,
		month_end_stamp,
	};
	return dat;
}

// 获取当月天数
const getNowMonthDayCount = () => {
	let date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	return (new Date(year, month, 0)).getDate();
}

// 获取一个月中每天的所有开始到结束的时间戳
const getTimeMonthTodayStamp = () => {
	let dat = [];
	let month_start_end_stamp = getTimeMonthStartEndStamp();
	let start_stamp = month_start_end_stamp.month_start_stamp;
	
	for(let i = 0; i < getNowMonthDayCount(); i++)
	{
		let tmp = {
			start_stamp: start_stamp + i * 24 * 60 * 60,
			end_stamp: start_stamp + (i + 1) * 24 * 60 * 60 - 1,
		};
		dat.push(tmp);
	}
	
	return dat;
}

// 将米转换为千米 0.00 格式
const meterFormatKm = (m) => {
	let km = (m / 1000).toFixed(2);
	return km;
}

// 返回包括今天在内的过去七天，每天的开始和结束时间戳
const getAgoWeekStartEndStamp = () => {
	// 当前日期到天
	let t = getNowTimeToDay();
	// 当天 0 点的时间戳
	t = (new Date(t)).getTime() / 1000 - (8 * 60 * 60);
	
	let dat = [];
	
	for(let i = 0; i < 7; i++)
	{
		let tmp = {
			start_stamp: t - i * 24 * 60 * 60,
			end_stamp: t - (i - 1) * 24 * 60 * 60 - 1,
		};
		dat.push(tmp);
	}
	
	return dat;
}



export default {
	numberToBoolean, // 将数值转换为Boolean类型
	booleanToNumber, // 将Boolean类型转换为数值类型
	timeFormat, // 将时间戳转换为时间
	timeToStamp, // 时间转换成时间戳（精确到秒）
	getTimeStamp, // 获取当前时间戳（精确到秒）
	getNowTimeToSecond, // 获取当前时间到秒
	getNowTime, // 获取当前时间
	getNowTimeToDay, // 获取当前日期到天
	getTimeStartEndStamp, // 获取某时间戳当天的开始和结束的时间戳
	getTimeWeekTodayStamp, // 获取一周中当天之前的所有开始到结束时间戳
	getTimeWeekStartEndStamp, // 获取本周的开始和结束时间戳
	getDayOfWeek, // 获取当天所属周几
	calcStampToNow, // 计算传入当前时间到传入时间戳的间隔天数
	stampFormatDay, // 将传入时间戳截取到天并格式化 yyyy-mm-dd
	secondFormatHours, // 将秒转为小时，--:--:--格式
	secondToHours, // 将秒转为小时，00.000
	getStampToYear, // 获取时间戳日期到年
	getStampToMonth, // 获取时间戳日期到月
	getStampToDay, // 获取时间戳日期到日
	getTimeMonthStartEndStamp, // 获取本月的开始和结束时间戳
	getNowMonthDayCount, // 获取当月天数
	getTimeMonthTodayStamp, // 获取一个月中每天的所有开始到结束的时间戳
	meterFormatKm, // 将米转换为千米 0.00 格式
	getAgoWeekStartEndStamp, // 返回包括今天在内的过去七天，每天的开始和结束时间戳
}